package com.tdam2011.grupo02.ui;

import com.tdam2011.grupo02.R;
import com.tdam2011.grupo02.compatibility.ActionBarActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

public class SendMessageErrorActivity extends ActionBarActivity{

	private static final int ID_ERROR = 1;
	private String error;
	private String destinatario;
	private String mensaje;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(getIntent().hasExtra("error"))
			error=getIntent().getExtras().getString("error");
		if(getIntent().hasExtra("destinatario"))
			destinatario=getIntent().getExtras().getString("destinatario");
		if(getIntent().hasExtra("mensaje"))
			mensaje=getIntent().getExtras().getString("mensaje");
		showDialog(ID_ERROR);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		if(id == ID_ERROR){
			dialog = createErrorDialog();
		} else{
			dialog = super.onCreateDialog(id);
		}
		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		if (id == ID_ERROR) {
			((AlertDialog) dialog).setMessage(error);
		}
	}
	
	private Dialog createErrorDialog(){
		
		DialogInterface.OnClickListener clickAceptar = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		};
		
		DialogInterface.OnClickListener clickReenviar = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent=new Intent(SendMessageErrorActivity.this,MensajeWebComposicionActivity.class);
				intent.putExtra("destinatario", destinatario);
				intent.putExtra("mensaje", mensaje);
				startActivity(intent);
				finish();
				
			}
		};


		Dialog dialog = new AlertDialog.Builder(this)
				.setIcon(R.drawable.quick_action_msgweb)
				.setTitle(getString(R.string.sendMessageErrorDialogTitle))
				.setPositiveButton(getString(R.string.sendMessageErrorDialogPosBtn), clickAceptar)
				.setNegativeButton(getString(R.string.sendMessageErrorDialogNegBtn),
						clickReenviar).setMessage(error)
				.create();
		return dialog;
	}
}
